package cn.tanghom.app.oa.service;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.flowable.common.engine.impl.el.AbstractFlowableFunctionDelegate;

import com.unbiz.common.StringUtil;
import com.unbiz.common.convert.primitive.IntegerConverter;

public class IntMatchFunctionDelegate extends AbstractFlowableFunctionDelegate {

	public static final Pattern AGE_UNDER = Pattern.compile("(\\d+).*[以内|以下]+");
	public static final Pattern AGE_UPPER = Pattern.compile("(\\d+).*[以上]+");

	public static final Pattern NUMBER = Pattern.compile("[\\d]+");
	public static final Pattern NUMERIC = Pattern.compile("-?\\d+\\.?\\d*");

	@Override
	public String prefix() {
		return "int";
	}

	@Override
	public String localName() {
		return "match";
	}

	@Override
	public Method functionMethod() {
		return getTwoObjectParameterMethod();
	}

	@Override
	public Class<?> functionClass() {

		return this.getClass();
	}

	/**
	 * int 范围匹配
	 * 
	 * 如： 22岁以上，3年以内，30万到40万
	 * 
	 * @param a
	 *            简历属性
	 * @param b
	 *            职位属性，
	 * @return
	 */
	public static boolean match(Object a, Object b) {
		// 有一个为空，则认为匹配
		if (a == null || b == null)
			return true;

		String astr = a.toString();
		String bstr = b.toString();
		if (astr.isEmpty() || bstr.isEmpty())
			return true;

		int aint = 0;
		int bint = 0;
		if (a instanceof Number) {
			aint = ((Number) a).intValue();
		} else {
			aint = Integer.parseInt(astr);
		}
		if (b instanceof Number) {
			bint = ((Number) b).intValue();
			if (aint == bint) {
				return true;
			}
			return false;
		} else if (b instanceof CharSequence) {
			String[] values = bstr.split("[-|到|~]+");
			if (values.length >= 2) {
				int min = IntegerConverter.parseInt(values[0]);
				int max = IntegerConverter.parseInt(values[1]);
				if (aint >= min && aint <= max) {
					return true;
				}
				return false;
			} else {
				String regexMax = getRegexString(bstr, AGE_UNDER);
				if (!regexMax.isEmpty()) {
					int max = IntegerConverter.parseInt(regexMax);
					if (aint <= max) {
						return true;
					}
					return false;
				} else {
					regexMax = getRegexString(bstr, AGE_UPPER);
					if (!regexMax.isEmpty()) {
						int min = IntegerConverter.parseInt(regexMax);
						if (aint >= min) {
							return true;
						}
						return false;
					} else {
						bint = IntegerConverter.parseInt(bstr);
						if (aint == bint) {
							return true;
						}
						return false;
					}
				}
			}
		}

		return false;
	}

	public static boolean isNumeric(String s) {
		if (StringUtil.isBlank(s))
			return false;
		return NUMERIC.matcher(s).matches();
	}

	public static boolean isInteger(String s) {
		if (StringUtil.isBlank(s))
			return false;
		return NUMBER.matcher(s).matches();
	}

	public static String getRegexString(String line, Pattern p) {
		if (StringUtil.isBlank(line)) {
			return "";
		}
		try {
			Matcher ma = p.matcher(line.replaceAll("\\s+", " "));
			while (ma.matches()) {
				return ma.group(1);
			}
			return ma.group(1);
		} catch (Exception e) {
			return "";
		}
	}
}
